<?php

namespace app\admin\controller\ba;

use app\admin\model\Branchpiles;
use app\admin\model\Branchseat;
use app\common\controller\Backend;

/**
 * 营地列管理
 */
class Branch extends Backend
{
    /**
     * Branch模型对象
     * @var object
     * @phpstan-var \app\admin\model\ba\Branch
     */
    protected object $model;

    protected array|string $preExcludeFields = ['id'];

    protected string|array $quickSearchField = ['id'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new \app\admin\model\ba\Branch;
    }


    /**
     * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
     */
    /**
     * 查看
     * @throws Throwable
     */
    public function index(): void
    {
        if ($this->request->param('select')) {
            $this->select();
        }

        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->model
            ->field($this->indexField)
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit);
        foreach ($res as $v){
            $v->seat_sum=(new Branchseat())->where('branch_id',$v->id)->count();
            $v->piles_sum=(new Branchpiles())->where('branch_id',$v->id)->count();
            if ($v->camp_pic){
                $v->camp_pic='http://zhuangtianxia.oss-cn-shanghai.aliyuncs.com/'.$v->camp_pic;
            }
            if ($v->small_pic){
                $v->small_pic='http://zhuangtianxia.oss-cn-shanghai.aliyuncs.com/'.$v->small_pic;
            }
            //字符串转数组
            $v->content=explode(',',$v->content);
            $v->content=array_map(function ($v){
                return 'http://zhuangtianxia.oss-cn-shanghai.aliyuncs.com/'.$v;
            },$v->content);
            //数组转字符串
            $v->content=implode(',',$v->content);
        }
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }
    /**
     * 编辑
     * @throws Throwable
     */
    public function edit(): void
    {
        $pk  = $this->model->getPk();
        $id  = $this->request->param($pk);
        $row = $this->model->find($id);
        if (!$row) {
            $this->error(__('Record not found'));
        }

        $dataLimitAdminIds = $this->getDataLimitAdminIds();
        if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
            $this->error(__('You have no permission'));
        }
        //字符串转数组
        $row->content=explode(',',$row->content);
        $row->content=array_map(function ($v){
            return 'http://zhuangtianxia.oss-cn-shanghai.aliyuncs.com/'.$v;
        },$row->content);
        if ($row->camp_pic){
            $row->camp_pic='http://zhuangtianxia.oss-cn-shanghai.aliyuncs.com/'.$row->camp_pic;
        }
        if ($row->small_pic){
            $row->small_pic='http://zhuangtianxia.oss-cn-shanghai.aliyuncs.com/'.$row->small_pic;
        }
        //数组转字符串
        $row->content=implode(',',$row->content);
        if ($this->request->isPost()) {
            $this->error(__('不可编辑'));
            $data = $this->request->post();
            if (!$data) {
                $this->error(__('Parameter %s can not be empty', ['']));
            }

            $data   = $this->excludeFields($data);
            $result = false;
            $this->model->startTrans();
            try {
                // 模型验证
                if ($this->modelValidate) {
                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    if (class_exists($validate)) {
                        $validate = new $validate;
                        if ($this->modelSceneValidate) $validate->scene('edit');
                        $data[$pk] = $row[$pk];
                        $validate->check($data);
                    }
                }
                $result = $row->save($data);
                $this->model->commit();
            } catch (Throwable $e) {
                $this->model->rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success(__('Update successful'));
            } else {
                $this->error(__('No rows updated'));
            }
        }

        $this->success('', [
            'row' => $row
        ]);
    }
}